package com.juyu.miliao.api.rabbitMQ;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.domain.GiftSpend;
import com.juyu.miliao.common.constant.FanoutExchangeKey;
import com.juyu.miliao.common.constant.GiftWallRabbitMqInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

/**
 * 礼物墙rabbitmq
 *
 * @author xuanqicheng
 * @create 2019-18-13:42
 */
@Component
@Slf4j
public class GiftWallRabbitMq {

    @Autowired
    private AmqpTemplate amqpTemplate;

    public void setGiftWallMessage(String message) {

        if (!StringUtils.isEmpty(message)) {
            GiftSpend giftSpend = JSONObject.parseObject(message, GiftSpend.class);
            if (giftSpend != null) {
                amqpTemplate.convertAndSend(GiftWallRabbitMqInfo.GIFT_WALL_EXCHANGE, GiftWallRabbitMqInfo.GIFT_WALL_KEY,
                        JSONObject.toJSONString(giftSpend));

            }
        }

    }


    public void sendGiftWallMessage(GiftSpend giftSpend) {
        if (giftSpend != null) {
            amqpTemplate.convertAndSend(FanoutExchangeKey.SEND_GIFT_FANOUT_EXCHANGE_KEY, "",
                    JSONObject.toJSONString(giftSpend));
        }
    }

}
